package com.finalcial.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.finalcial.dao.PowerDao;
import com.finalcial.service.PowerService;

@Service
public class PowerServiceImpl implements PowerService {
	
	@Autowired
	private PowerDao powerDao;
	
	/**
	 * 进入权限管理界面显示用户信息----查询所有用户信息
	 * @return 用户id、用户姓名、部门名
	 */
	@Override
	public List<Map<String, Object>> listUserSketchyMsg() {
		List<Map<String, Object>> list = powerDao.listUserSketchyMsg();
		return list;
	}
	/**
	 * 进入权限管理界面显示角色信息----显示用户角色
	 * @return 角色id、角色名
	 */
	@Override
	public List<Map<String, Object>> listRoleName() {
		List<Map<String, Object>> list = powerDao.listRoleName();
		return list;
	}
	
	/**
	 * 通过父ID查询子节点
	 * @param parentId 父节点ID
	 * @param nodes 返回值json对象
	 * @return
	 */
	@Override
	public String getUserRoles(Integer parentId, String nodes, String roleId) {
		List<Map<String, Object>> children = powerDao.getChildren(parentId);
		List<Map<String, Object>> functionOne = powerDao.getChildrenByType(parentId, roleId);
		Integer tt = 1;
		if(children.size() > 0) {
			for(Map<String, Object> cl : children) {
				nodes += "{";
				for(String s :cl.keySet()) {
					nodes += s+":'"+cl.get(s)+"',";
				}
				nodes += "spread:true,";
				for(Map<String, Object> fo : functionOne) {
					if(cl.get("functionId").equals(fo.get("functionId"))) {
						tt = 0;
					}
				}
				if(tt == 0) {
					nodes += "checked:true";
				}else {
					nodes += "checked:false";
				}
				nodes += getUserRoles((Integer)cl.get("functionId"),",children:[", roleId);
				nodes += "},";
			}
			nodes = nodes.substring(0, nodes.length()-1);
			nodes = nodes.replace("functionId","id");
			nodes = nodes.replace("functionName","name");
			nodes += "]";
		}else {
			nodes = "";
		}
		return nodes;
	}
	
}
